package com.banzhi.shop.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.data.ChoiceData;
import com.banzhi.shop.data.PinTuanBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.banzhi.shop.MyApp.context;

/**
 * Created by Administrator on 2018/11/9.
 */
//
public class MyChoiceAdapter extends RecyclerView.Adapter<MyChoiceAdapter.ViewHolder> {
    private List<ChoiceData.GoodsBean> mList;
    private MyChoiceAdapter.OnItemClickListener mOnItemClickListener = null;
    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        View myView;
        TextView price;
        ImageView imageView;
        TextView titles;
        TextView lirun;
         LinearLayout mLlItem;

        public ViewHolder(View itemView) {
            super(itemView);
            myView = itemView;

            titles = (TextView) itemView.findViewById(R.id.tv_home_contents);
            lirun = (TextView) itemView.findViewById(R.id.tv_liyun111);
            imageView = (ImageView) itemView.findViewById(R.id.img_home111);
            price = (TextView) itemView.findViewById(R.id.tv_price111);
            mLlItem = (LinearLayout) itemView.findViewById(R.id.ll_ban);

        }
    }

    public MyChoiceAdapter(List<ChoiceData.GoodsBean> list) {
        this.mList = list;
    }

    @Override
    public MyChoiceAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items4, null);
        final MyChoiceAdapter.ViewHolder holder = new MyChoiceAdapter.ViewHolder(view);
        //条目点击控件
        holder. mLlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(MyChoiceAdapter.ViewHolder holder, int position) {
        ChoiceData.GoodsBean bean = mList.get(position);
        holder.titles.setText(bean.getTitle());
        holder.price.setText(bean.getMarketprice());
        holder.lirun.setText(bean.getProfit());

        Picasso.with(context).load(bean.getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.imageView);
        // bt4申请售后 bt5待发货 bt6确认收货

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    //下面两个方法提供给页面刷新和加载时调用
    public void add(List<ChoiceData.GoodsBean> addMessageList) {
        //增加数据
        int position = mList.size();
        mList.addAll(position, addMessageList);
        notifyItemInserted(position);
    }

    public void refresh(List<ChoiceData.GoodsBean> newList) {
        //刷新数据
        mList.removeAll(mList);
        mList.addAll(newList);
        notifyDataSetChanged();
    }
    public void setOnItemClickListener(MyChoiceAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
